<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        var obj1 = {
            name: "卡莎",
            age: 22,
            skill: {
                name1: "虚空索敌",
                name2: "艾卡西亚暴雨"
            }
        }
        //需求：创建深拷贝函数，深拷贝obj1到obj2

        function deepCopy(obj1) {
            // var obj2 = {}
            // 判断实参是数组还是对象
            var obj2 = Object.prototype.toString.call(obj1) === '[object Array]' ? [] : {}
            for (item in obj1) {
                // for in循环会循环到原型链上的属性，所以需要限制
                if (obj1.hasOwnProperty(item)) {
                    // 判断obj1的属性是否存在引用数据类型
                    if (typeof obj1[item] === 'object') {
                        obj2[item] = deepCopy(obj1[item])
                    } else {
                        obj2[item] = obj1[item]
                    }
                }
            }
            return obj2
        }

        var obj2 = deepCopy(obj1)
        obj2.age = 11
        obj2.skill.name1 = 11
        console.log(obj2)
        console.log(obj1)





    </script>
</body>

</html>